package com.zhangzy.qm.dao;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.json.JSONObject;

import com.zhangzy.qm.entity.Ablum;
import com.zhangzy.qm.entity.Option;
import com.zhangzy.qm.entity.Temp;
import com.zhangzy.qm.entity.Topic;
import com.zhangzy.qm.util.Constants;
import com.zhangzy.qm.util.FileUtil;
import com.zhangzy.qm.util.ZipUtil;

public class TopicDao extends BaseDao<Topic> {

	public List<Topic> queryTopicList(String hql){
		return super.getHibernateTemplate().find(hql);
	}
	
	public List<Topic> query(String sql){
		Session session = getSession();
		Query query = session.createQuery(sql);
		List<Topic> list = query.list();
		session.close();	
		return list;
	}
	public List<Topic> query(String sql,int start,int pageSize){
		Session session = getSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<Topic> list = query.list();
		session.close();	
		return list;
	}
	

	
	public int queryCount(String sql){
		Session session = getSession();
		Query query = session.createQuery(sql);
		Long count = (Long) query.uniqueResult();
		session.close();	
		return count.intValue();	
	}
	
	public int queryId(String sql){
		Session session = getSession();
		Query query = session.createQuery(sql);
		Integer count = (Integer) query.uniqueResult();
		session.close();	
		return count;	
	}
	
	public Map<String,Integer> queryMap(String hql){
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(hql);
		query.addScalar("key1",Hibernate.STRING);
		query.addScalar("value1",Hibernate.INTEGER);
		query.setResultTransformer(Transformers.aliasToBean(Temp.class));
		List<Temp> list = query.list();
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		for(Temp t:list){			
			map.put(t.getKey1(), t.getValue1());
		}
		session.close();	
		return map;
	}

}
